package com.bootdo.system.service.impl;

import com.bootdo.system.dao.BizhongDao;
import com.bootdo.system.dao.JiaoyicelueDao;
import com.bootdo.system.domain.BizhongDO;
import com.bootdo.system.domain.FrontuserDO;
import com.bootdo.system.domain.JiaoyiVO;
import com.bootdo.system.domain.JiaoyicelueDO;
import com.bootdo.system.service.JiaoyicelueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class JiaoyicelueServiceImpl implements JiaoyicelueService {
	@Autowired
	private JiaoyicelueDao jiaoyicelueDao;
	@Autowired
	private BizhongDao bizhongDao;


    @Override
    public void saveCelue(FrontuserDO user, BizhongDO bizhongDO) {
		JiaoyicelueDO jiaoyicelueDO = new JiaoyicelueDO();
		jiaoyicelueDO.setUserid(user.getUserid());
		jiaoyicelueDO.setBizhongid(bizhongDO.getId());
		jiaoyicelueDO.setChicangjine(BigDecimal.ZERO);
		jiaoyicelueDO.setBucangcishu(0);
		jiaoyicelueDO.setZhangfu(BigDecimal.ZERO);
		jiaoyicelueDO.setFirstamount(new BigDecimal(50));//首单50u
		jiaoyicelueDO.setBeitoushangxian(7);//倍投上限7单
		jiaoyicelueDO.setZhiyingbili(new BigDecimal(0.013));//止盈比例1.3%
		jiaoyicelueDO.setZhiyinghuidiao(new BigDecimal(0.002));//止盈回调0.2%
		jiaoyicelueDO.setBucangdiefu(new BigDecimal(0.033));//补仓跌幅3.3%
		jiaoyicelueDO.setBucanghuidiao(new BigDecimal(0.003));//补仓回调0.3%
		jiaoyicelueDO.setXunhuantype(0);
		jiaoyicelueDO.setCeluestatus(0);
		jiaoyicelueDO.setChicangjunjia(BigDecimal.ZERO);
		jiaoyicelueDO.setChicangshuliang(BigDecimal.ZERO);

		save(jiaoyicelueDO);
    }

    @Override
	public JiaoyicelueDO get(Long id){
		return jiaoyicelueDao.get(id);
	}
	
	@Override
	public List<JiaoyicelueDO> list(Map<String, Object> map){
		return jiaoyicelueDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return jiaoyicelueDao.count(map);
	}
	
	@Override
	public int save(JiaoyicelueDO jiaoyicelue){
		return jiaoyicelueDao.save(jiaoyicelue);
	}

	@Transactional
	@Override
	public int update(JiaoyicelueDO jiaoyicelue){
		return jiaoyicelueDao.update(jiaoyicelue);
	}
	
	@Override
	public int remove(Long id){
		return jiaoyicelueDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return jiaoyicelueDao.batchRemove(ids);
	}

    @Override
    public List<JiaoyiVO> listwithKeyword(Long userid,String keyword) {

		return jiaoyicelueDao.listwithKeyword( userid,keyword);
    }

	@Transactional
	@Override
	public void batchSave(Long userid) {
		 List<BizhongDO> list = bizhongDao.list(new HashMap<>());
		for (BizhongDO b :list) {
			JiaoyicelueDO jiaoyicelueDO = new JiaoyicelueDO();
			jiaoyicelueDO.setUserid(userid);
			jiaoyicelueDO.setBizhongid(b.getId());
			jiaoyicelueDO.setChicangjine(BigDecimal.ZERO);
			jiaoyicelueDO.setBucangcishu(0);
			jiaoyicelueDO.setZhangfu(BigDecimal.ZERO);
			jiaoyicelueDO.setFirstamount(BigDecimal.ZERO);
			jiaoyicelueDO.setBeitoushangxian(0);
			jiaoyicelueDO.setZhiyingbili(BigDecimal.ZERO);
			jiaoyicelueDO.setZhiyinghuidiao(BigDecimal.ZERO);
			jiaoyicelueDO.setBucangdiefu(BigDecimal.ZERO);
			jiaoyicelueDO.setBucanghuidiao(BigDecimal.ZERO);
			jiaoyicelueDO.setXunhuantype(0);
			jiaoyicelueDO.setCeluestatus(0);
			jiaoyicelueDO.setChicangjunjia(BigDecimal.ZERO);
			jiaoyicelueDO.setChicangshuliang(BigDecimal.ZERO);

			jiaoyicelueDao.save(jiaoyicelueDO);
		}
	}

    @Override
    public void updateByUserIdAndBizhongid(JiaoyicelueDO jiaoyicelueDO) {

		jiaoyicelueDao.updateByUserIdAndBizhongid(jiaoyicelueDO);
    }

	@Transactional(propagation = Propagation.REQUIRES_NEW)
	@Override
	public void updateids(Long[] ids, Long userid) {

		jiaoyicelueDao.updateids(ids,userid);
	}

	@Transactional
	@Override
	public void updateByUserIdAndBizhongid2(JiaoyicelueDO jiaoyicelueDO) {

		jiaoyicelueDao.updateByUserIdAndBizhongid2( jiaoyicelueDO);
	}

    @Override
    public List<JiaoyicelueDO> getList() {

		return jiaoyicelueDao.getList();
    }

    @Override
    public JiaoyicelueDO getMairuPriceByUidAndBid(Long userid, Integer bizhongid) {

		return jiaoyicelueDao.getMairuPriceByUidAndBid(userid, bizhongid);
    }

    @Override
    public JiaoyicelueDO getCelueByUserIdAndBizhongId(Long userid, Integer id) {

		return jiaoyicelueDao.getCelueByUserIdAndBizhongId(userid, id);
	}

	@Transactional
    @Override
    public void updateZhangfu(JiaoyicelueDO jiaoyicelueDO) {
        jiaoyicelueDao.updateZhangfu(jiaoyicelueDO);
    }

	@Override
	public Integer updateByids(Long[] ids, Long userid) {
		return  jiaoyicelueDao.updateByids(ids,  userid);
	}

	@Override
	public int updateCelueByIds(JiaoyicelueDO jiaoyicelueDO) {

    	return jiaoyicelueDao.updateCelueByIds( jiaoyicelueDO);
	}

}
